package com.lhk;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


public class FlowSortMapReduce {

    /***
     * mapper静态内部类
     * @author Administrator
     *
     */
    public static class FlowSortMapper extends Mapper<LongWritable, Text, FlowBean, NullWritable> {

        //拿到一行数据，切分出各字段，封装为一个flowbean，作为key输出
        @Override
        protected void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
            //获取到一行数据
            String line = value.toString();
            //对这一行数据进行截取
            String[] fields = StringUtils.split(line, "      ");

            //获取数据里面的数据
            String phoneNumber = fields[0];
            long up_flow = Long.parseLong(fields[7]);
            long down_flow = Long.parseLong(fields[8]);

            //将数据进行封装传递给reduce
            context.write(new FlowBean(phoneNumber, up_flow, down_flow), NullWritable.get());
        }

    }

    /***
     * reducer的静态内部类
     * @author Administrator
     *
     */
    public static class FlowSortReducer extends Reducer<FlowBean, NullWritable, Text, FlowBean> {

        @Override
        protected void reduce(FlowBean key, Iterable<NullWritable> values, Context context)
                throws IOException, InterruptedException {

            String phoneNumber = key.getPhoneNumber();
            context.write(new Text(phoneNumber), key);
        }
    }


    /***
     * 主方法
     * @param args
     * @throws InterruptedException
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static void main(String[] args) throws ClassNotFoundException, IOException, InterruptedException {
        //创建配置文件
        Configuration conf = new Configuration();
        //获取一个作业
        Job job = Job.getInstance(conf);

        //设置整个job所用的那些类在哪个jar包
        job.setJarByClass(FlowSortMapReduce.class);

        //本job使用的mapper和reducer的类
        job.setMapperClass(FlowSortMapper.class);
        job.setReducerClass(FlowSortReducer.class);

        //指定mapper的输出数据key-value类型
        job.setMapOutputKeyClass(FlowBean.class);
        job.setMapOutputValueClass(NullWritable.class);

        //指定reduce的输出数据key-value类型Text
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(FlowBean.class);

        //指定要处理的输入数据存放路径
        //FileInputFormat是所有以文件作为数据源的InputFormat实现的基类，
        //FileInputFormat保存作为job输入的所有文件，并实现了对输入文件计算splits的方法。
        //至于获得记录的方法是有不同的子类——TextInputFormat进行实现的。
        FileInputFormat.setInputPaths(job, new Path(args[0]));

        //指定处理结果的输出数据存放路径
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //将job提交给集群运行
        //job.waitForCompletion(true);
        //正常执行成功返回0，否则返回1
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}